﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///HelpDes 的摘要说明
/// </summary>
public partial class HelpDes
{

    public static HelpDes Get(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.HelpDes.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<HelpDes> GetAllConditon(int HelpNavaID,string Key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from h in context.HelpDes
                        select h;
            if (HelpNavaID != 0)
                query = query.Where(row => row.NavaID == HelpNavaID);
            if (string.IsNullOrEmpty(Key))
                query = query.Where(row => row.Title.Contains(Key) || row.Des.Contains(Key));
            return query.OrderByDescending(row=>row.Rank).ToList();
        }
    }


    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.HelpDes.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<HelpDes>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.HelpDes.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.HelpDes.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
}